package com.life.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JColorChooser;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JCheckBox;

import com.life.constants.Constants;

import java.awt.Color;

public class NeighborColorPanel extends JPanel {

	WidgetRegistry widgetRegistry = WidgetRegistry.getInstance();
	ColorGenerator theColorGenerator = ColorGenerator.getInstance();
	ColorChooserButton colorButton0 = null;
	ColorChooserButton colorButton1 = null;
	ColorChooserButton colorButton2 = null;
	ColorChooserButton colorButton3 = null;
	ColorChooserButton colorButton4 = null;
	ColorChooserButton colorButton5 = null;
	ColorChooserButton colorButton6 = null;
	ColorChooserButton colorButton7 = null;
	ColorChooserButton colorButton8 = null;
	JCheckBox applyAllCheckBox = null;
	ColorChooserButton applyAllColorButton = null;
	
	JLabel label1 = null;
	JLabel label2 = null;
	JLabel label3 = null;
	JLabel label4 = null;
	JLabel label5 = null;
	JLabel label6 = null;
	JLabel label7 = null;
	JLabel label8 = null;
	JLabel label9 = null;
	
	
	public NeighborColorPanel()
	{
		this.setLayout(null);
		TitledBorder neighborTitle = new TitledBorder("Neighbor Coloring");
		this.setBorder(neighborTitle);
		
		int xOffset = 10;
		int yOffset = 30;
		int width = 10;
		int height = 10;
		int index = 1;
		int buffer = 5;
		int colorBuffer = 10;
		int colorWidth = 100;
		int colorHeight = 20;
		
		applyAllColorButton = new ColorChooserButton(Color.white,-1);
		applyAllColorButton.setBounds(190,50,70,20);
		applyAllColorButton.setEnabled(false);
		this.add(applyAllColorButton);
		
		JPanel applyAllPanel = new JPanel();
		TitledBorder applyAllTitle = new TitledBorder("Apply All");
		applyAllPanel.setBorder(applyAllTitle);
		applyAllPanel.setBounds(150,25,150,70);
		applyAllCheckBox = new JCheckBox();
		applyAllCheckBox.setBounds(160,50,20,20);
		applyAllCheckBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				boolean selected = applyAllCheckBox.isSelected();
				if(selected)
				{
					for(int i = 0; i < 9;i++)
					{
						theColorGenerator.setColor(i,applyAllColorButton.getColor());
					}
					repaint();
				}
				else
				{
				    theColorGenerator.setColor(0,colorButton0.getColor());
					theColorGenerator.setColor(1,colorButton1.getColor());
					theColorGenerator.setColor(2,colorButton2.getColor());
					theColorGenerator.setColor(3,colorButton3.getColor());
					theColorGenerator.setColor(4,colorButton4.getColor());
					theColorGenerator.setColor(5,colorButton5.getColor());
					theColorGenerator.setColor(6,colorButton6.getColor());
					theColorGenerator.setColor(7,colorButton7.getColor());
					theColorGenerator.setColor(8,colorButton8.getColor());
				}
				
				applyAllColorButton.setEnabled(selected);
				colorButton0.setEnabled(!selected);
				colorButton1.setEnabled(!selected);
				colorButton2.setEnabled(!selected);
				colorButton3.setEnabled(!selected);
				colorButton4.setEnabled(!selected);
				colorButton5.setEnabled(!selected);
				colorButton6.setEnabled(!selected);
				colorButton7.setEnabled(!selected);
				colorButton8.setEnabled(!selected);
				
				label1.setEnabled(!selected);
				label2.setEnabled(!selected);
				label3.setEnabled(!selected);
				label4.setEnabled(!selected);
				label5.setEnabled(!selected);
				label6.setEnabled(!selected);
				label7.setEnabled(!selected);
				label8.setEnabled(!selected);
				label9.setEnabled(!selected);
			}			
			
		});
		
		
		this.add(applyAllCheckBox);
		
		this.add(applyAllPanel);
				
		 label1 = new JLabel("0");
		label1.setBounds(xOffset,yOffset*index++ +buffer,width,height);

		colorButton0 = new ColorChooserButton(theColorGenerator.getNeighborColor(0),0);
		colorButton0.setBounds((int)(label1.getBounds().getMaxX())+colorBuffer,(int)(label1.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton0);


		 label2 = new JLabel("1");
		label2.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton1 = new ColorChooserButton(theColorGenerator.getNeighborColor(1),1);
		colorButton1.setBounds((int)(label2.getBounds().getMaxX())+colorBuffer,(int)(label2.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton1);

		 label3 = new JLabel("2");
		label3.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton2 = new ColorChooserButton(theColorGenerator.getNeighborColor(2),2);
		colorButton2.setBounds((int)(label3.getBounds().getMaxX())+colorBuffer,(int)(label3.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton2);


		 label4 = new JLabel("3");
		label4.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton3 = new ColorChooserButton(theColorGenerator.getNeighborColor(3),3);
		colorButton3.setBounds((int)(label4.getBounds().getMaxX())+colorBuffer,(int)(label4.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton3);

		 label5 = new JLabel("4");
		label5.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton4 = new ColorChooserButton(theColorGenerator.getNeighborColor(4),4);;
		colorButton4.setBounds((int)(label5.getBounds().getMaxX())+colorBuffer,(int)(label5.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton4);

		 label6 = new JLabel("5");
		label6.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton5 = new ColorChooserButton(theColorGenerator.getNeighborColor(5),5);
		colorButton5.setBounds((int)(label6.getBounds().getMaxX())+colorBuffer,(int)(label6.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton5);

		 label7 = new JLabel("6");
		label7.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton6 = new ColorChooserButton(theColorGenerator.getNeighborColor(6),6);
		colorButton6.setBounds((int)(label7.getBounds().getMaxX())+colorBuffer,(int)(label7.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton6);


		 label8 = new JLabel("7");
		label8.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton7 = new ColorChooserButton(theColorGenerator.getNeighborColor(7),7);
		colorButton7.setBounds((int)(label8.getBounds().getMaxX())+colorBuffer,(int)(label8.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton7);

		 label9 = new JLabel("8");
		label9.setBounds(xOffset,yOffset*index++ +buffer,width,height);
		colorButton8 = new ColorChooserButton(theColorGenerator.getNeighborColor(8),8);
		colorButton8.setBounds((int)(label9.getBounds().getMaxX())+colorBuffer,(int)(label9.getBounds().getMinY())-buffer,colorWidth,colorHeight);
		this.add(colorButton8);
		
		JLabel backgroundLabel = new JLabel("Background");
		
		this.add(label1);
		this.add(label2);
		this.add(label3);
		this.add(label4);
		this.add(label5);
		this.add(label6);
		this.add(label7);
		this.add(label8);
		this.add(label9);
		
	    
		
	}
		
	
	class ColorChooserButton extends JButton implements ActionListener, ChangeListener
	{
			private Color theColor = Color.red;
			JColorChooser chooser = null;
			JFrame popupFrame = null;
			int width = 500;
			int height =450;
			int neighborValue = 0;
			
			public ColorChooserButton(Color color, int neighbor)
			{
				setColor(color);
				neighborValue = neighbor;
				init();
	
			}
			
			
			protected void init()
			{
				this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.HAND_CURSOR));

				this.addActionListener(this);
				chooser = new JColorChooser();
				popupFrame = new JFrame();
				Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
				popupFrame.getContentPane().setBounds((int)((screenDim.getWidth()-width)/2),(int)((screenDim.getHeight()-height)/2),width,height);
				popupFrame.setBounds((int)((screenDim.getWidth()-width)/2),(int)((screenDim.getHeight()-height)/2),width,height);
				popupFrame.getContentPane().add(chooser);

				chooser.getSelectionModel().addChangeListener(this);
				
				if(!Constants.IS_APPLET)
				{
				    popupFrame.setAlwaysOnTop(true);
				}
				
				
				if(neighborValue == -1)
				{
					popupFrame.setTitle("All Neighbor Colors");
				}
				else
				{
					popupFrame.setTitle(neighborValue + " Neighbor Color");
				}
				

			}

			public Color getColor() {
				return theColor;
			}

			public void setColor(Color theColor) {
				this.theColor = theColor;
			}
			
			public void paint(Graphics g)
			{
				super.paint(g);
				
				if(this.isEnabled())
				{
				
				g.setColor(getColor());
				g.fillRect(0,0,this.getWidth(),this.getHeight());
		
				g.setColor(Color.black);
				g.drawRect(0,0,this.getWidth()-1,this.getHeight()-1);
				}
//				else
//				{
//					g.setColor(Color.LIGHT_GRAY);
//					g.fillRect(0,0,this.getWidth(),this.getHeight());
//			
//					g.setColor(Color.DARK_GRAY);
//					g.drawRect(0,0,this.getWidth()-1,this.getHeight()-1);
//				}
			}
			
			public void actionPerformed(ActionEvent event)
			{
				chooser.setColor(theColor);
				popupFrame.setVisible(true);
				
			}
			
			public void stateChanged(ChangeEvent event)
			{
				setColor(chooser.getColor());
				
				if(neighborValue != -1)
				{
					ColorGenerator.getInstance().setColor(neighborValue, getColor());
				}
				else if(neighborValue == -1 && applyAllCheckBox.isSelected())
				{
					for(int i = 0; i < 9;i++)
					{
						theColorGenerator.setColor(i,applyAllColorButton.getColor());
					}
				}
				repaint();
			}
	}
	
}
